package com.miao.im.core.exception;

import cn.hutool.extra.spring.SpringUtil;
import com.miao.im.server.session.SessionContext;
import com.miao.im.support.exception.BizException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/3/19 17:30
 * @Description:
 */
public class SocketServerExceptionBus {

    /**
     * 执行异常处理
     * @author miaoguoxin
     * @date 2021/3/19
     */
    public static Method invoke(SessionContext sessionContext, Throwable cause) {
        if (sessionContext == null || cause == null) {
            return null;
        }
        SocketServerExceptionMethodResolver resolver = SpringUtil.getBean(SocketServerExceptionMethodResolver.class);
        Method method = resolver.resolveMethodByExceptionType(cause.getClass());
        if (method == null) {
            return null;
        }
        Object adviceBean = resolver.getAdviceBean();
        try {
            method.invoke(adviceBean, (RuntimeException)cause, sessionContext);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return method;
    }
}
